package com.mooer.app;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mooer.R;

/**
 * @author pengfei.xu
 * @version 创建时间：2011-6-22 下午01:15:11 类说明
 */
public class ActivityListAdapter extends BaseAdapter {

	private Context ctx;

	private List<Map<String, Object>> list;

	private boolean isNew;

	public ActivityListAdapter(Context ctx, List<Map<String, Object>> list,
			boolean isNew) {
		this.ctx = ctx;
		this.list = list;
		this.isNew = isNew;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View itemView = (LinearLayout) LayoutInflater.from(ctx).inflate(
				R.layout.activity_list_item, null);
		LinearLayout layout1 = (LinearLayout) itemView
				.findViewById(R.id.layout1);
		LinearLayout layout2 = (LinearLayout) itemView
				.findViewById(R.id.layout2);
		TextView date = (TextView) itemView.findViewById(R.id.activity_date);
		final TextView require = (TextView) itemView
				.findViewById(R.id.activity_require);
		TextView join = (TextView) itemView.findViewById(R.id.join_num);
		TextView repeat = (TextView) itemView.findViewById(R.id.repeat_num);
		ImageView pic = (ImageView) itemView.findViewById(R.id.activity_pic);
		if (isNew) {
			layout1.setVisibility(View.GONE);
			layout2.setVisibility(View.VISIBLE);
			join.setText("活动即将开始");
			repeat.setText("敬请期待");
		} else {
			date.setText((String) list.get(position).get("date"));
			join.setText(ctx.getString(R.string.join_number, (Integer) list
					.get(position).get("join")));
			repeat.setText(ctx.getString(R.string.repeat_number, (Integer) list
					.get(position).get("repeat")));
		}
		require.setText((String) list.get(position).get("require"));
		Button select = (Button) itemView.findViewById(R.id.select);
		itemView.setTag(position);
		select.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.putExtra("activityId",
						(Integer) list.get(position).get("activityId"));
				intent.putExtra("title", (String) list.get(position)
						.get("name"));
				intent.putExtra("content",
						(String) list.get(position).get("require"));
				if (isNew) {
					intent.setClass(ctx, ActivityDetail2.class);
					ctx.startActivity(intent);
				} else {
					intent.setClass(ctx, ActivityDetail.class);
					ctx.startActivity(intent);
				}

			}
		});
		itemView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				int position = (Integer) v.getTag();
				Intent intent = new Intent();
				intent.putExtra("activityId",
						(Integer) list.get(position).get("activityId"));
				intent.putExtra("title", (String) list.get(position)
						.get("name"));
				intent.putExtra("content",
						(String) list.get(position).get("require"));
				if (isNew) {
					intent.setClass(ctx, ActivityDetail2.class);
					ctx.startActivity(intent);
				} else {
					intent.setClass(ctx, ActivityDetail.class);
					ctx.startActivity(intent);
				}
			}
		});
		return itemView;
	}

}
